<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 300px;
            margin: 100px auto;
            text-align: center;
        }
        span {
            display: inline-block;
            width: 50px;
            height: 50px;
            line-height: 50px;
            background: #000;
            color: white;
        }
    </style>
</head>
<body>
    <div>
       <span class="hour"></span>
       <span class="minute"></span>
       <span class="second"></span>
    </div>
    <script>
       var hour = document.querySelector('.hour');
       var minute = document.querySelector('.minute');
       var second = document.querySelector('.second');
       var inputTime = +new Date('2021-3-10 18:00:00');//返回用户输入时间的总毫秒数
       countDown();//事先调用一次这个函数，防止第一次刷新页面有空白
       setInterval(countDown,1000);//开始定时器

       function countDown(time){
            var nowTime = +new Date(); //返回当前时间的总毫秒数
            var times = (inputTime - nowTime) / 1000;//剩余时间的总的秒数
            var h = parseInt(times / 60 /60 % 24);//时
            h = h < 10 ? '0'+h:h;
            hour.innerHTML = h;
            var m = parseInt(times / 60 % 60);//分
            m = m < 10 ? '0'+ m : m;
            minute.innerHTML = m;
            var s = parseInt(times % 60);//秒
            s = s < 10 ? '0'+ s : s;
            second.innerHTML = s;
        }
    </script>
</body>
</html>